# encoding:utf8
import paramiko

class SFTP(object):
    def __init__(self,ip,user,psd):
        self.user=user
        self.ip=ip
        self.psd = psd

    #执行远程指令
    def run(self,cmd):
        ssh=paramiko.SSHClient()
        try:
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(self.ip,username=self.user,password=self.psd)
            for m in cmd:
                stdin,stdout,stderr=ssh.exec_command(m)
                print(stdout.read())     
        except:
            print("%s\tError:%s\n"%(self.ip,''.join(cmd)))
        ssh.close()

    #检测目录
    def exist_dir(self,dir_path):
        ssh=paramiko.SSHClient()
        try:
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(self.ip,username=self.user,password=self.psd)
            sftp = ssh.open_sftp()
            sftp.listdir(dir_path)
            ssh.close()
            print("exist")
            return True
        except:
            ssh.close()
            print("not exist")
            return False
        
    #创建目录
    def mkdir(self,dir_path):
        ssh=paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(self.ip,username=self.user,password=self.psd)
        sftp = ssh.open_sftp()
        try:
            sftp.listdir(dir_path)
            print("%s:exist"%dir_path)
        except:
            try:
                stdin,stdout,stderr=ssh.exec_command('mkdir -p %s'%dir_path)
                print('%s:creation success'%dir_path) 
            except:
                print('%s:creation failed'%dir_path)
        sftp.close()
        ssh.close()

    #上传本地文件
    def upload(self,uSRC,uDST):
        # ssh=paramiko.SSHClient()
        # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        # ssh.connect(self.ip,username=self.user,password=self.psd)
        # sftp = ssh.open_sftp()
        # #远程路径不存在则创建
        # parent_path=None
        # try:
        #     parent_path = uDST[:uDST.rfind('/')]          
        #     sftp.listdir(parent_path)
        # except:
        #     stdin,stdout,stderr=ssh.exec_command('mkdir -p %s'%parent_path)
        #     print('%s:creation success'%parent_path) 
        # print(uSRC, uDST)
        # print(sftp.listdir(parent_path))

        transport = paramiko.Transport((self.ip, 22))
        transport.connect(username=self.user, password=self.psd)
        sftp = paramiko.SFTPClient.from_transport(transport)
        sftp.put(uSRC, uDST)#上传
        sftp.close()#关闭连接
        print("Upload local file : \"%s\" to Host : %s \"%s\"\n" %(uSRC,self.ip,uDST))

    #从远程主机下载文件到本地主机
    def download(self,dSRC,dDST):
        transport = paramiko.Transport((self.ip, 22))
        transport.connect(username=self.user, password=self.psd)
        sftp = paramiko.SFTPClient.from_transport(transport)
        sftp.get(dSRC,dDST)#将Linux上的/root/Linux.txt下载到本地
        transport.close()
        print("Download remote file from : \"%s\" to : %s \"%s\"\n" %(dDST,self.ip,dSRC))

if __name__=='__main__':
    sftp = SFTP('10.116.38.13','root','qxfw.3179@3813')
    #sftp.run(['mkdir -p /home/l2/l3',])
    sftp.upload('/home/tu/temp','temp.png')
